<?php


namespace app\common\model;


use think\facade\Cache;
use think\facade\Db;

class Cate
{

    public static function setSort($user, $cate_type, $sort_data)
    {
//        $sort_data = json_encode($sort_data);
        $sort = Db::name("user_cate_sort")->where("user_id", $user['id'])
            ->where('cate_type', $cate_type)
            ->find();

        if(empty($sort)){
            Db::name("user_cate_sort")->insert([
                "user_id"=>$user['id'],
                "cate_type"=>$cate_type,
                "cate_sort"=>$sort_data
            ]);
        }else{
            Db::name("user_cate_sort")->where("user_id", $user['id'])
                ->where('cate_type', $cate_type)
                ->update([
                    "cate_sort"=>$sort_data
                ]);
        }
    }


    //获取分类对应图标
    public static function getCateImg($user_id, $cate_id = 0)
    {
        $cate_img = Db::name("user_cate")->where("user_id", "in", [0, $user_id])
            ->cache(20)
            ->column("img", "id");
        if ($cate_id) {
            return $cate_img[$cate_id];
        } else {
            return $cate_img;
        }
    }

    //修改类目
    public static function editCate($user_id, $cate_id, $cate_name, $img)
    {
        $cate = Db::name("user_cate")->where('user_id', $user_id)
            ->where('id', $cate_id)
            ->find();
        if (empty($cate)) {
            throw new UserException('类别不存在或为系统类别不支持修改');
        }
        Db::name("user_cate")->where('user_id', $user_id)
            ->where('id', $cate_id)
            ->update([
                "cate_name" => $cate_name,
                "img" => $img
            ]);
    }

    //添加类目
    public static function addCate($user_id, $cate_type, $cate_name, $img, $cate_id)
    {
        if(empty($cate_id)){
            Db::name("user_cate")->insert([
                "user_id" => $user_id,
                "cate_name" => $cate_name,
                "cate_type" => $cate_type,
                "create_time" => time(),
                "sort_num" => 1,
                "img" => $img
            ]);
        }else{
            $cate = Db::name("user_cate")->where("id", $cate_id)->find();
            if(empty($cate)){
                throw new UserException('分类不存在');
            }
            if($cate['default_type'] == 1){
                Db::name("user_default_cate")->insert([
                    "user_id"=>$user_id,
                    "cate_id"=>$cate_id,
                    "create_time"=>time()
                ]);
            }else{
                $del = Db::name("user_del_cate")
                    ->where("user_id", $user_id)
                    ->where("del_cate_id", $cate_id)
                    ->delete();
            }
        }
    }

    //删除账目类别
    public static function delCate($user_id, $cate_id)
    {
        $cate = Db::name("user_cate")
            ->where('user_id', 'in', [0, $user_id])
            ->where('id', $cate_id)
            ->find();
        if (empty($cate)) {
            throw new UserException('类别不存在');
        }
        if ($cate['user_id'] == 0) {
            if($cate['default_type'] == 1){
                Db::name("user_default_cate")
                    ->where("user_id", $user_id)
                    ->where("cate_id", $cate_id)
                    ->delete();
            }else{
                $del = Db::name("user_del_cate")
                    ->where("user_id", $user_id)
                    ->where("del_cate_id", $cate_id)
                    ->find();
                if (!$del) {
                    Db::name("user_del_cate")->insert([
                        "user_id" => $user_id,
                        "del_cate_id" => $cate_id,
                        "create_time" => time()
                    ]);
                }
            }
        } else {
            Db::name("user_cate")
                ->where('user_id', $user_id)
                ->where('id', $cate_id)
                ->delete();
        }
    }

    //获取账目分类
    public static function getCateList($user_id, $cate_type)
    {
        $del_cate = self::userDelId($user_id);
        //已经添加的系统分类
        $exist_cate_id = Db::name("user_default_cate")->where("user_id", $user_id)->column("cate_id");


        //收入分类排序
        $income_sort = Db::name("user_cate_sort")->where('user_id', $user_id)
            ->where('cate_type', 2)
            ->value('cate_sort');

        if(!empty($income_sort)){
            $st = json_decode($income_sort, true);
            $sort_arr = [];
            foreach ($st as $sa) {
                $sort_arr[$sa['cate_id']] = $sa['sort_num'];
            }
        }
        //收入分类
        $income_start = 1;
        $income_cate1 = Db::name("user_cate")
            ->where('user_id', 'in', ['0', $user_id])
            ->where('default_type', 0)
            ->whereNotIn('id', $del_cate)
            ->where('cate_type', 2)
            ->field('cate_name, img, id cate_id')
            ->select()
            ->toArray();
        $income_cate2 = Db::name("user_cate")
            ->where('user_id', 'in', ['0', $user_id])
            ->where('default_type', 1)
            ->where('cate_type', 2)
            ->whereIn('id', $exist_cate_id)
            ->field('cate_name, img, id cate_id')
            ->select()
            ->toArray();
        $income_cate = array_merge($income_cate1, $income_cate2);
        foreach ($income_cate as &$val) {
            if ($income_sort) {
                if (array_key_exists($val['cate_id'], $sort_arr)) {
                    $val['sort_num'] = $sort_arr[$val['cate_id']];
                } else {
                    $val['sort_num'] = $income_start;
                    $income_start++;
                }
            } else {
                $val['sort_num'] = $income_start;
                $income_start++;
            }
        }
        unset($val);
        $last_names = array_column($income_cate, 'sort_num');
        array_multisort($last_names, SORT_ASC, $income_cate);
        //默认未添加的分类
        $income_default1 = Db::name("user_cate")
            ->where('user_id', 0)
            ->where('default_type', 1)
            ->where('cate_type', 2)
            ->whereNotIn("id", $exist_cate_id)
            ->field('cate_name, img, id cate_id, 0 sort_num')
            ->select()
            ->toArray();
        $income_default2 = Db::name("user_cate")
            ->where('user_id', 0)
            ->where('default_type', 0)
            ->where('cate_type', 2)
            ->whereIn("id", $del_cate)
            ->field('cate_name, img, id cate_id, 0 sort_num')
            ->select()
            ->toArray();
        $income_default = array_merge($income_default2, $income_default1);



        //支出分类排序
        $outlay_sort = Db::name("user_cate_sort")->where('user_id', $user_id)
            ->where('cate_type', 1)
            ->value('cate_sort');
        if(!empty($outlay_sort)){
            $st = json_decode($outlay_sort, true);
            $sort_arr = [];
            foreach ($st as $sa) {
                $sort_arr[$sa['cate_id']] = $sa['sort_num'];
            }
        }
        //支出分类
        $outlay_start = 1;
        //支出分类
        $outlay_cate1 = Db::name("user_cate")
            ->where('user_id', 'in', ['0', $user_id])
            ->where('default_type', 0)
            ->whereNotIn('id', $del_cate)
            ->where('cate_type', 1)
            ->field('cate_name, img, id cate_id')
            ->select()
            ->toArray();
        $outlay_cate2 = Db::name("user_cate")
            ->where('user_id', 'in', ['0', $user_id])
            ->where('default_type', 0)
            ->whereNotIn('id', $del_cate)
            ->where('cate_type', 1)
            ->whereIn("id", $del_cate)
            ->field('cate_name, img, id cate_id')
            ->select()
            ->toArray();
        $outlay_cate = array_merge($outlay_cate1, $outlay_cate2);
        foreach ($outlay_cate as &$val) {
            if ($outlay_sort) {
                if (array_key_exists($val['cate_id'], $sort_arr)) {
                    $val['sort_num'] = $sort_arr[$val['cate_id']];
                } else {
                    $val['sort_num'] = $outlay_start;
                    $outlay_start++;
                }
            } else {
                $val['sort_num'] = $outlay_start;
                $outlay_start++;
            }
        }
        unset($val);
        $last_names1 = array_column($outlay_cate, 'sort_num');
        array_multisort($last_names1, SORT_ASC, $outlay_cate);
        //默认未添加的分类
        $outlay_default1 = Db::name("user_cate")
            ->where('user_id', 0)
            ->where('default_type', 1)
            ->where('cate_type', 1)
            ->whereNotIn("id", $exist_cate_id)
            ->field('cate_name, img, id cate_id, 0 sort_num')
            ->select()
            ->toArray();
        $outlay_default2 = Db::name("user_cate")
            ->where('user_id', 0)
            ->where('default_type', 0)
            ->where('cate_type', 1)
            ->whereIn("id", $del_cate)
            ->field('cate_name, img, id cate_id, 0 sort_num')
            ->select()
            ->toArray();
        $outlay_default = array_merge($outlay_default2, $outlay_default1);



        $user_cate = [
            "income_cate" => [
                "use_cate" => $income_cate,
                "nouse_cate" => $income_default
            ],
            "outlay_cate" => [
                "use_cate" => $outlay_cate,
                "nouse_cate" => $outlay_default
            ]
        ];
//        $del_cate = self::userDelId($user_id);
//        $user_cate = Db::name("user_cate")
//            ->where('id', 'not in', $del_cate)
//            ->where('user_id', 'in', ['0', $user_id])
//            ->where('cate_type', $cate_type)
//            ->field('cate_name, img, id cate_id')
//            ->select()->toArray();
        return $user_cate;
    }

    //用户已删除分类id
    public static function userDelId($user_id)
    {
        return Db::name("user_del_cate")
            ->where('user_id', $user_id)
            ->column('del_cate_id');
    }


}